class Renderer {
    constructor(canvas) {
        this.canvas = canvas;
        this.ctx = canvas.getContext('2d');
        this.blockSize = 14; // 增加方块大小
        this.gridColor = '#0f380f';
        this.backgroundColor = '#9bbc0f';

        // 增加画布大小
        this.canvas.width = 240;
        this.canvas.height = 216;
    }

    // 清空画布
    clear() {
        this.ctx.fillStyle = this.backgroundColor;
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
    }

    // 绘制游戏板
    drawBoard(gameBoard) {
        // 调整偏移量，确保游戏板居中显示
        const offsetX = (this.canvas.width - gameBoard.cols * this.blockSize) / 2;
        const offsetY = 15; // 增加顶部偏移，让内容居中显示

        // 绘制背景网格
        this.ctx.strokeStyle = this.gridColor;
        this.ctx.lineWidth = 0.5;

        for (let row = 0; row <= gameBoard.rows; row++) {
            this.ctx.beginPath();
            this.ctx.moveTo(offsetX, offsetY + row * this.blockSize);
            this.ctx.lineTo(offsetX + gameBoard.cols * this.blockSize, offsetY + row * this.blockSize);
            this.ctx.stroke();
        }

        for (let col = 0; col <= gameBoard.cols; col++) {
            this.ctx.beginPath();
            this.ctx.moveTo(offsetX + col * this.blockSize, offsetY);
            this.ctx.lineTo(offsetX + col * this.blockSize, offsetY + gameBoard.rows * this.blockSize);
            this.ctx.stroke();
        }

        // 绘制已放置的方块
        for (let row = 0; row < gameBoard.rows; row++) {
            for (let col = 0; col < gameBoard.cols; col++) {
                const color = gameBoard.grid[row][col];
                if (color) {
                    this.drawBlock(col, row, color, offsetX, offsetY);
                }
            }
        }
    }

    // 绘制当前方块
    drawBlock(x, y, color, offsetX, offsetY) {
        this.ctx.fillStyle = this.convertToGameBoyColor(color);
        this.ctx.fillRect(
            offsetX + x * this.blockSize,
            offsetY + y * this.blockSize,
            this.blockSize,
            this.blockSize
        );

        // 绘制方块边框
        this.ctx.strokeStyle = this.gridColor;
        this.ctx.lineWidth = 0.5;
        this.ctx.strokeRect(
            offsetX + x * this.blockSize,
            offsetY + y * this.blockSize,
            this.blockSize,
            this.blockSize
        );
    }

    // 绘制活动方块
    drawActiveBlock(block) {
        // 确保使用与drawBoard相同的偏移量
        const offsetX = (this.canvas.width - 10 * this.blockSize) / 2;
        const offsetY = 2; // 与drawBoard保持一致

        const cells = block.getCells();
        for (const cell of cells) {
            this.drawBlock(cell.x, cell.y, block.color, offsetX, offsetY);
        }
    }

    // 绘制下一个方块预览
    drawNextBlock(block) {
        const offsetX = this.canvas.width - 40;
        const offsetY = 20;

        this.ctx.fillStyle = this.gridColor;
        this.ctx.font = '8px Arial';
        this.ctx.fillText('NEXT', offsetX - 5, offsetY - 5);

        const shape = block.getCurrentShape();
        for (let row = 0; row < shape.length; row++) {
            for (let col = 0; col < shape[row].length; col++) {
                if (shape[row][col]) {
                    this.drawBlock(col, row, block.color, offsetX, offsetY);
                }
            }
        }
    }

    // 绘制分数和等级
    drawStats(score, level, linesCleared) {
        this.ctx.fillStyle = this.gridColor;
        this.ctx.font = '8px Arial';

        this.ctx.fillText(`SCORE: ${score}`, 10, 10);
        this.ctx.fillText(`LEVEL: ${level}`, 10, 20);
        this.ctx.fillText(`LINES: ${linesCleared}`, 10, 30);
    }

    // 将RGB颜色转换为GameBoy的4种绿色
    convertToGameBoyColor(color) {
        // GameBoy只有4种绿色
        const gameBoyColors = [
            '#0f380f', // 最暗
            '#306230', // 暗
            '#8bac0f', // 亮
            '#9bbc0f'  // 最亮
        ];

        // 简单地根据颜色的亮度选择一个GameBoy颜色
        switch (color) {
            case '#FF0000': return gameBoyColors[0]; // 红色 -> 最暗
            case '#00FF00': return gameBoyColors[2]; // 绿色 -> 亮
            case '#0000FF': return gameBoyColors[1]; // 蓝色 -> 暗
            case '#FFFF00': return gameBoyColors[2]; // 黄色 -> 亮
            case '#00FFFF': return gameBoyColors[3]; // 青色 -> 最亮
            case '#FF7F00': return gameBoyColors[1]; // 橙色 -> 暗
            case '#800080': return gameBoyColors[0]; // 紫色 -> 最暗
            default: return gameBoyColors[1];
        }
    }
}